// pages/cart/cart.js

const req = require('../../utils/req.js');
const {
  api
} = require('../../utils/api.js');

// 删除后重置状态
var initdata = function(that) {
  var shopcarData = that.data.shopcarData
  for (var i = 0; i < shopcarData.length; i++) {
    shopcarData[i].index = ""
  }
  that.setData({
    shopcarData: shopcarData
  })
}
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    adminShow: false, //管理      
    shopcarData: [], //购物车数据      
    total: 0, //总金额      
    allsel: false, //全选      
    selarr: [], //选择的货物      
    hintText: '', //提示的内容      
    hintShow: false, //是否显示提示
    productIds: [],
    isChecked: 0,
    allse: false,

  },

  //点击全选  
  allcheckTap: function() {
    let shopcar = this.data.shopcarData,
      //点击全选后allsel变化
      allsel = !this.data.allsel,
      total = 0;
    for (let i = 0, len = shopcar.length; i < len; i++) {
      //所有商品的选中状态和allsel值一样
      shopcar[i].checked = allsel;
      //如果为选中状态则计算商品的价格 
      if (allsel) {
        total += shopcar[i].market_price * shopcar[i].number;
      }
    }
    //如果选中状态为true那么所有商品为选中状态，将物品加入选中变量，否则为空
    this.data.selarr = allsel ? shopcar : [];
    this.setData({
      allsel: allsel,
      shopcarData: shopcar,
      total: total,
      selarr: this.data.selarr
    });
  },
  //点击移除商品  
  deleteshopTap: function() {
    var allsel = this.data.allsel,
      shopcar = this.data.shopcarData,
      selarr = this.data.selarr;
    if (allsel) {
      shopcar = [];
      this.setData({
        allsel: false
      });
    } else {
      console.log('移除', selarr);
      //将选中的商品从购物车移除  
      for (var i = 0, len = selarr.length; i < len; i++) {
        // console.log(selarr[i].id);
        for (var lens = shopcar.length - 1, j = lens; j >= 0; j--) {
          // console.log(shopcar[j].id);
          if (selarr[i].index == shopcar[j].index) {
            shopcar.splice(j, 1);
          }
        }
      }
    }
    this.setData({
      shopcarData: shopcar,
      total: 0
    });
  },
  //点击删除按钮事件 
  delItem: function(e) {
   
    let cart_delete = api.cart_delete
    cart_delete.data.productIds = String(e.currentTarget.dataset.goodsId)
    req.ask(cart_delete).then(res => {
      // console.log(res)
      console.log(res)
      this.setData({
        shopcarData: res.data.data.cartList
      })
    })
    initdata(this)
  },

  //点击管理按钮，是否显示管理的选项  
  adminTap: function() {
    this.setData({
      adminShow: !this.data.adminShow
    });
  },
  //点击单个选择按钮  
  checkTap: function(e) {
    // console.log(e.detail.value[0])))
    // e.detail.value[0] = e.detail.value[0] == 0 ? 1 : 0;

    let index = e.currentTarget.dataset.index; // 商品列表id
    let id = e.currentTarget.dataset.id;

    let cart_checked = api.cart_checked;


    cart_checked.data.productIds = id
    cart_checked.data.isChecked = e.detail.value[0]
    req.ask(cart_checked).then(res => {
      console.log('商品id', res.data.data.cartList);

      this.setData({
        shopcarData: res.data.data.cartList
      })


      let shopcar = this.data.shopcarData,
        allse = !this.data.allse, //点击选择后allse变化
        total = 0;
      // for (let i = 0, len = shopcar.length; i < len; i++) {
      shopcar[index].checked = allse; //所有商品的选中状态和allsel值一样
      if (allse) { //如果为选中状态则计算商品的价格
        total += shopcar[index].retail_price * shopcar[index].number;
      }
      // }
      //如果选中状态为true那么所有商品为选中状态，将物品加入选中变量，否则为空
      this.data.selarr = allse ? shopcar : [];
      this.setData({
        allse: allse,
        shopcarData: shopcar,
        total: total,
        selarr: this.data.selarr
      });


    })

  },
  //点击加减按钮  
  numchangeTap: function(e) {
    console.log('123', this.data.total)
    let Index = e.currentTarget.dataset.index, //点击的商品下标值        
      shopcar = this.data.shopcarData,
      types = e.currentTarget.dataset.types, //是加号还是减号        
      total = this.data.total; //总计    
    switch (types) {
      case 'add':
        shopcar[Index].number++; //对应商品的数量+1      
        shopcar[Index].checked && (total += parseInt(shopcar[Index].retail_price)); //如果商品为选中的，则合计价格+商品单价      
        break;
      case 'minus':
        shopcar[Index].number--; //对应商品的数量-1      
        shopcar[Index].checked && (total -= parseInt(shopcar[Index].retail_price)); //如果商品为选中的，则合计价格-商品单价      
        break;
    }
    this.setData({
      shopcarData: shopcar,
      total: total
    });
  },
  //判断是否为全选  
  judgmentAll: function() {
    let shopcar = this.data.shopcarData,
      shoplen = shopcar.length,
      lenIndex = 0; //选中的物品的个数    
    for (let i = 0; i < shoplen; i++) { //计算购物车选中的商品的个数    
      shopcar[i].checked && lenIndex++;
    }
    this.setData({
      //如果购物车选中的个数和购物车里货物的总数相同，则为全选，反之为未全选
      allsel: lenIndex == shoplen
    });
  },
  // 跳转到订单详情页
  goclearingTap: function(e) {
    console.log('eeeeeee',e)
    let id = e.currentTarget.dataset.id;
    let cart_checkout = api.cart_checkout;


    cart_checkout.data.goods_id = id
    // cart_checked.data.isChecked = e.detail.value[0]
    req.ask(cart_checkout).then(res => {
      console.log('订单', res);

    //   this.setData({
    //     shopcarData: res.data.data.cartList
    //   })
    })
    wx.navigateTo({
      url: './order/order',
    })
  },
  shopData:function(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../index/goodsDetail/goodsDetail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('666');
    this.init()
  },

  init() {
    req.ask(api.cart_index).then(res => {
      console.log('购物车', res);
      this.setData({
        shopcarData: res.data.data.cartList
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.init()
    var shopcarData = this.data.shopcarData,
      total = 0,
      selarr = this.data.selarr;
    //这里是对选中的商品的价格进行总结
    for (let i = 0, len = shopcarData.length; i < len; i++) {
      if (shopcarData[i].check) {
        total += shopcarData[i].number * shopcarData[i].retail_price;
        selarr.push(shopcarData[i].index);
      }
    }
    this.setData({
      shopcarData: shopcarData,
      total: total,
      selarr: selarr
    });
    this.judgmentAll(); //判断是否全选  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})